﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Coffee.Lists
{
    public partial class Lst_table : DevExpress.XtraEditors.XtraForm
    {
        cls_Table tbl;
        public Lst_table()
        {
            InitializeComponent();
            tbl = new cls_Table();
        }

        private void Lst_table_Load(object sender, EventArgs e)
        {
            try
            {
                Lists.Lst_Position cl_vt = new Lst_Position();
                cbViTri.Properties.DataSource = cl_vt.GetAll();
                cbViTri.Properties.PopulateColumns();
                cbViTri.Properties.Columns["PositionID"].Visible = false;

                grid.AppendMenu(barDel);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void bar_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa dòng dữ liệu này không ?", "Xác nhận !",
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) return;

            DataRow row = view.GetFocusedDataRow();
            if (object.Equals(row, null)) return;
            tbl.DeleteRow(row);
            view.DeleteSelectedRows();
        }

        private void cbViTri_Properties_EditValueChanged(object sender, EventArgs e)
        {
            grid.SetData(tbl.GetDataByPositionId(TSoft.Cvert.ToInt32(cbViTri.EditValue, 0)));
        }

        private void btCreate_Click(object sender, EventArgs e)
        {
            if (nuNumber.Value <= 0) return;
            DataTable Data = (DataTable)grid.DataSource;
            if (Data == null) return;

            for (int i = 0; i < nuNumber.Value; i++)
            {
                DataRow row = Data.NewRow();
                row["Name"] = (i + 1).ToString();
                row["PositionID"] = TSoft.Cvert.ToInt32(cbViTri.EditValue, 0);
                Data.Rows.Add(row);
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable Data = (DataTable)grid.DataSource;
                if (Data == null) return;
                tbl.Update(ref Data);
                btSave.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void view_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            btSave.Enabled = true;
        }
    }
}
